package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.controller.vo.AddCategoryReq;
import com.imooc.mall.controller.vo.UpdateCategoryReq;
import com.imooc.mall.entity.User;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.service.ICategoryService;
import com.imooc.mall.service.IUserService;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhaoJianHui
 * @since 2025-05-07
 */
@RestController
public class CategoryController {
    @Autowired
    IUserService userService;
    @Autowired
    ICategoryService categoryService;
    /**
     * 添加分类
     *
     * @param addCategoryReq
     * @return
     */
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(@Valid @RequestBody AddCategoryReq addCategoryReq) {
        categoryService.add(addCategoryReq);
        return ApiRestResponse.success();
    }
    /**
     * 更新分类
     *
     * @param updateCategoryReq
     * @return
     */
    @PostMapping("/admin/category/update")
    @ResponseBody
    public  ApiRestResponse updateCategory(@Valid @RequestBody UpdateCategoryReq updateCategoryReq) {
        categoryService.upCategory(updateCategoryReq);
        return ApiRestResponse.success();
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@Valid @NotNull @RequestParam("id") Integer id) {
        boolean remove = categoryService.removeById(id);
        if (!remove) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        return ApiRestResponse.success();
    }
}
